package test5;

public class miantest {
    public static void main(String[] args) {
        //前三个学生添加
        Student[] arr = new Student[3];   
        Student stu1 = new Student(1, "张三", 20);
        Student stu2 = new Student(2, "李四", 21);
        Student stu3 = new Student(3, "王五", 22);
        arr[0] = stu1;
        arr[1] = stu2;
        arr[2] = stu3;
        Student stu4 = new Student(4, "赵六", 25);

        boolean flag = contains(arr, stu4.getId());
        if (flag == true) {
            System.out.println("当前ID重复，请重新修改后添加");
        }
        else {
            int count = getcount(arr);
            if(getcount(arr)== arr.length){
                Student[] newArr = creatNewArr(arr);
                newArr[count] = stu4;
                printArr(newArr);
        }
            else{
                arr[count] = stu4;
                printArr(arr);
            }
        }
    }
    public static void printArr(Student[] arr){
        for (int i = 0; i < arr.length; i++) {
            Student stu = arr[i];
            if(arr[i]!=null){
                System.out.println(stu.getId()+","+ stu.getName()+","+ stu.getAge());
            }
        }
    }

    public static Student[] creatNewArr(Student[] arr){
        Student[] newArr = new Student[arr.length+1];
        for (int i = 0; i < arr.length; i++) {
            newArr[i] = arr[i];
        }
        return newArr;
    }
    public static boolean contains(Student[] arr, int id) {
        for (int i = 0; i < arr.length; i++) {
            Student stu = arr[i];
            if (stu != null) {
                int sid = stu.getId();
                if (sid == id) {
                    return true;
                }
            }
        }
        return false;
    }
    //看数组中是否有空余
    public static int getcount(Student[] arr){
        int count =0;
        for (int i = 0; i < arr.length; i++) {
            if(arr[i]!=null){
                count++;
            }
        }
        return count;
    }
}
